import axios from 'axios';
import {MessageBox} from "element-ui";

export function request(config){
    const instance = axios.create({
        baseURL: 'http://120.79.14.226:8081',
        timeout: 5000,

    });
    //请求拦截器
    instance.interceptors.request.use(config=>{
      //在请求头中添加token
      config.headers.Authorization = window.localStorage.getItem("springBoot-token");
      return config;
    },err=>{
      return err;
    });
    //响应拦截器
    instance.interceptors.response.use(res=>{
      if(res.data.code==401){   //token过期，重新登录
        MessageBox.alert(res.data.msg, "请点击确认按钮跳转登录页面", {
          confirmButtonText: '确定',
          callback: action => {
            localStorage.removeItem("springBoot-token");
            localStorage.removeItem("role");
            window.location.href='/'
          }
        });
        // window.vm.$alert(res.data.msg,"请点击确认按钮跳转登录页面",{
        //   confirmButtonText: '确定'
        // }).then( ()=>{
        //  window.vm.$router.push("/");
        // })
      }
      else {
        return res.data;  //返回data数据
      }
    },err=>{
      return err;
    })
    return instance(config);
}

export function requestExcel(config){
  const instance = axios.create({
    baseURL: 'http://120.79.14.226:8081',
    timeout: 5000,
  });
  //请求拦截器
  instance.interceptors.request.use(config=>{
    //在请求头中添加token
    config.headers.Authorization = window.localStorage.getItem("springBoot-token");
    return config;
  },err=>{
    return err;
  });
  instance.interceptors.response.use(res=>{
    if(res.data.code==401){   //token过期，重新登录
      MessageBox.alert(res.data.msg, "请点击确认按钮跳转登录页面", {
        confirmButtonText: '确定',
        callback: action => {
          localStorage.clear();
          window.location.href='/'
        }
      });
    }
    else {
      return res;  //返回res
    }
  },err=>{
    return err;
  })
  return instance(config);
}
